package ynu.edu.elmspringboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.elmspringboot.po.DeliveryAddress;
import ynu.edu.elmspringboot.service.DeliveryAddressService;

import java.util.List;

@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @PostMapping("/listDeliveryAddressByUserId")
    public List<DeliveryAddress> listDeliveryAddressByUserId(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.listDeliveryAddressByUserId(deliveryAddress.getUserId());
        return deliveryAddressService.lambdaQuery().eq(DeliveryAddress::getUserId, deliveryAddress.getUserId()).list();
    }

    @PostMapping("/getDeliveryAddressById")
    public DeliveryAddress getDeliveryAddressById(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.getDeliveryAddressById(deliveryAddress.getDaId());
        return deliveryAddressService.getById(deliveryAddress.getDaId());
    }

    @PostMapping("/saveDeliveryAddress")
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
        return deliveryAddressService.save(deliveryAddress) ? 1 : 0;
    }

    @PostMapping("/updateDeliveryAddress")
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        return deliveryAddressService.updateById(deliveryAddress) ? 1 : 0;
    }

    @PostMapping("/removeDeliveryAddress")
    public int removeDeliveryAddress(DeliveryAddress deliveryAddress) {
//        return deliveryAddressService.removeDeliveryAddress(deliveryAddress.getDaId());
        return deliveryAddressService.removeById(deliveryAddress.getDaId()) ? 1 : 0;
    }
}
